package com.feeyo.groundservice.checkin.dlg

import android.content.Context
import android.view.View
import com.feeyo.groundservice.base_util.DateFormatType
import com.feeyo.groundservice.base_util.FyLog
import com.feeyo.groundservice.base_util.formatTimeHM
import com.feeyo.groundservice.base_util.formatTimeHMS
import com.feeyo.groundservice.base_util.formatTimeMD
import com.feeyo.groundservice.base_util.formatTimeY
import com.feeyo.groundservice.base_util.formatTimeYM
import com.feeyo.groundservice.base_util.formatTimeYMD
import com.feeyo.groundservice.base_util.formatTimeYMDH
import com.feeyo.groundservice.base_util.formatTimeYMDHM
import com.feeyo.groundservice.base_util.formatTimeYMDHMS
import com.feeyo.groundservice.base_util.toDate
import com.feeyo.groundservice.base_util.toSeconds
import com.feeyo.groundservice.time_address.listener.TimePickerListener
import com.feeyo.groundservice.time_address.popup.TimePickerPopup
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import java.util.Calendar
import java.util.Date

/**
 *作者：70893 on 2025-10-28
 *描述：
 */

interface SelectTimeListener {
    /**
     * @param time 时间戳，单位s
     * **/
    fun onSelectTime(date: String, time: Long)
}

abstract class BaseBottomCalendarPop(private val mContext: Context) : BottomPopupView(mContext) {

    private val TAG = javaClass.simpleName
    private val currentCalendar = Calendar.getInstance()//当前日历
    private var pickerMode = TimePickerPopup.Mode.YMDHM//年月日时分

    //时间格式化模板
    private var timeFormat = DateFormatType.FORMAT_YYYY_MM_DD_HH_MM

    //回调
    private var selectTimeListener: SelectTimeListener? = null


    /**初始化pickMode**/
    protected abstract fun setPickMode()


    override fun onCreate() {
        super.onCreate()
        setPickMode()
        updateTimeFormat()
    }

    /**根据mode更新一下timeFormat**/
    private fun updateTimeFormat() {
        timeFormat = when (pickerMode) {
            TimePickerPopup.Mode.Y -> DateFormatType.FORMAT_YYYY
            TimePickerPopup.Mode.YM -> DateFormatType.FORMAT_YYYY_MM
            TimePickerPopup.Mode.YMD -> DateFormatType.FORMAT_YYYY_MM_DD
            TimePickerPopup.Mode.YMDH -> DateFormatType.FORMAT_YYYY_MM_DD_HH
            TimePickerPopup.Mode.YMDHM -> DateFormatType.FORMAT_YYYY_MM_DD_HH_MM
            TimePickerPopup.Mode.YMDHMS -> DateFormatType.FORMAT_DEFAULT
            TimePickerPopup.Mode.MD -> DateFormatType.FORMAT_DEFAULT
            TimePickerPopup.Mode.HM -> DateFormatType.FORMAT_DEFAULT
            TimePickerPopup.Mode.HMS -> DateFormatType.FORMAT_DEFAULT
        }
    }

    private fun getCalendarDate(): TimePickerPopup = TimePickerPopup(mContext).apply {
        setDefaultDate(currentCalendar)
        setMode(pickerMode)
        setCyclic(false)
        setTimePickerListener(object : TimePickerListener {
            override fun onTimeChanged(date: Date?) {

            }

            override fun onTimeConfirm(date: Date?, view: View?) {
                date?.let {
                    val d = (it.time / 1000).let {
                        val s = when (pickerMode) {
                            TimePickerPopup.Mode.Y -> it.formatTimeY()
                            TimePickerPopup.Mode.YM -> it.formatTimeYM()
                            TimePickerPopup.Mode.YMD -> it.formatTimeYMD()
                            TimePickerPopup.Mode.YMDH -> it.formatTimeYMDH()
                            TimePickerPopup.Mode.YMDHM -> it.formatTimeYMDHM()
                            TimePickerPopup.Mode.YMDHMS -> it.formatTimeYMDHMS()
                            TimePickerPopup.Mode.MD -> it.formatTimeMD()
                            TimePickerPopup.Mode.HM -> it.formatTimeHM()
                            TimePickerPopup.Mode.HMS -> it.formatTimeHMS()

                        }
                        s
                    }
                    selectTimeListener?.onSelectTime(d, it.time.toSeconds())
                }
            }

            override fun onCancel() {

            }

        })
    }


    fun setPickMode(mode: TimePickerPopup.Mode) {
        pickerMode = mode
    }

    fun setSelectTimeCallback(listener: SelectTimeListener) {
        selectTimeListener = listener
    }

    //日期弹框
    fun showDatePicker(date: String? = null) {
        date?.let {
            updateCalendar(it)
        }
        getCalendarDate().let {
            XPopup.Builder(mContext)
                .isDestroyOnDismiss(true)
                .asCustom(it).show()
        }

    }

    //日期选中 更新日历日期,
    private fun updateCalendar(date: String) {
        if (date.length != timeFormat.length) {
            FyLog.e(TAG, "时间格式错误")
            return
        }
        currentCalendar.time = date.toDate(timeFormat) ?: Date()
    }
}